<?php
/**
 * Created by PhpStorm.
 * User: qinzhi
 * Date: 2017/07/10
 * Time: 11:01
 */
class Service_Clerk{

    /**
     * 获取店员详情信息
     * @param $openid
     * @return array|bool|mixed|null|string
     */
    public static function getClerkInfo($openid){
        $key = 'clerk_datail_' . $openid;
        $clerkDetail = FCache::get($key);
        if(!$clerkDetail){
            $clerkT = new FTable('clerk','','xhs_slave');
            $clerkDetail = $clerkT->where(array('openid'=>$openid))->find();
            FCache::set($key,$clerkDetail,3600);
        }
        return $clerkDetail;
    }

    /**
     * 清空店员详情缓存
     * @param $openid
     */
    public static function clearClerkInfoCache($openid){
        $key = 'clerk_datail_' . $openid;
        FCache::delete($key);
    }

    /**
     * 获取店员列表
     * @param int $shop_id
     * @return array
     */
    public static function getClerkList($shop_id){
        $clerkT = new FTable('clerk','','xhs_slave');
        $clerkList = $clerkT->where(array('shop_id'=>$shop_id))->select();
        return $clerkList;
    }

    /**
     * 保存店员
     * @param $result
     * @param $clerk
     * @return array
     */
    public static function saveClerk($result,$clerk = array()){
        $clerkT = new FTable('clerk','','xhs_master');
        $clerk['nickname'] = $result['nickname'];
        $clerk['avatar'] = $result['headimgurl'];
        $clerk['openid'] = $result['openid'];
        $clerk['gender'] = $result['sex'];
        $clerk['shop_id'] = $result['shop_id'];
        if(isset($result['subscribe'])) $clerk['subscribe'] = 1;
        if(isset($result['subscribe_time'])) $clerk['subscribe_time'] = $result['subscribe_time'];
        if(empty($clerk['uid'])){
            $clerk['create_time'] = date('Y-m-d H:i:s');
            $clerk['username'] = $clerk['nickname'];
            $uid = $clerkT->insert($clerk);
        }else{
            $uid = $clerk['uid'];
            unset($clerk['uid']);
            $clerkT->where(array('uid'=>$clerk['uid']))->save($clerk);
            self::clearClerkInfoCache($result['openid']);
        }

        $clerk['uid'] = $uid;
        return $clerk;
    }

    /**
     * 根据openid取消关注
     * @param $openid
     * @return bool
     */
    public static function cancelSubscribe($openid)
    {
        $table = new FTable('clerk','','xhs_master');
        $result = $table->where(array('openid' => $openid))->update1(array('subscribe' => 0));
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 激活关注
     * @param $openid
     * @return bool
     */
    public static function activationSubscribe($openid)
    {
        $table = new FTable('clerk','','xhs_master');
        $result = $table->where(array('openid' => $openid))->update1(array('subscribe' => 1,'subscribe_time'=>time()));
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据用户ID更新最后操作时间
     * @param $openid
     * @return bool
     */
    public static function updateLastTimeByUid($openid)
    {
        $table = new FTable('clerk','','xhs_master');
        $result = $table->where(array('openid' => $openid))->update1(array('last_operation_time' => time() + 60 * 60 * 24 * 2));
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}